package com.yh.wcx.controller;

import com.yh.wcx.Constant.MyConstant;
import com.yh.wcx.entity.User;
import com.yh.wcx.service.UserService;
import com.yh.wcx.to.UserTo;
import com.yh.wcx.util.R;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;


    @PostMapping("/login")
    @ResponseBody
    public R login(@RequestBody UserTo userTo, HttpSession session) {
        if (userTo.getUserName() == null || userTo.getUserName().equals("")) {
            return R.error(MyConstant.LOGIN_MESSAGE_FAIL_TIP);
        }

        if (userTo.getPassWord() == null || userTo.getPassWord().equals("")) {
            return R.error(MyConstant.LOGIN_MESSAGE_FAIL_TIP);
        }

        User user = userService.login(userTo);
        if (user != null) {
            session.setAttribute("login",user);
            return R.ok(MyConstant.LOGIN_MESSAGE_SUCCESS, user);
        } else {
            return R.ok(MyConstant.LOGIN_MESSAGE_FAIL);
        }
    }
}
